package ahblue.cn.tanzi.ui.person;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;

import java.util.ArrayList;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.util.DensityUtils;
import ahblue.cn.tanzi.view.ScaleTransitionPagerTitleView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author pink-jackstraw
 * @date 2018/11/10
 * @describe 我的订单
 */
public class OrderActivity extends BaseActivity {
    @BindView(R.id.magicIndicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    private ArrayList<OrderFrangment> fragments;
    private ArrayList<String> mTitleDataList;

    public static void into(Activity activity) {
        activity.startActivity(new Intent(activity, OrderActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        ButterKnife.bind(this);
        initMagic();
    }

    private void initMagic() {
        fragments = new ArrayList<OrderFrangment>();
        for (int type = 0; type < 5; type++) {
            fragments.add(OrderFrangment.newInstance(type));
        }
        mTitleDataList = new ArrayList<String>();
        mTitleDataList.add(getResources().getString(R.string.all));
        mTitleDataList.add(getResources().getString(R.string.report));
        mTitleDataList.add(getResources().getString(R.string.zone));
        mTitleDataList.add(getResources().getString(R.string.seal));
        mTitleDataList.add(getResources().getString(R.string.store));

        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {

            @Override
            public int getCount() {
                return mTitleDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                ScaleTransitionPagerTitleView colorTransitionPagerTitleView = new ScaleTransitionPagerTitleView(context);
//                colorTransitionPagerTitleView.setMinScale(0.9f);
                colorTransitionPagerTitleView.setNormalColor(getResources().getColor(R.color.grey));
                colorTransitionPagerTitleView.setSelectedColor(getResources().getColor(R.color.blue_press));
                colorTransitionPagerTitleView.setTextSize(15);
                colorTransitionPagerTitleView.setText(mTitleDataList.get(index));
                colorTransitionPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        viewpager.setCurrentItem(index);
                    }
                });
                return colorTransitionPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_MATCH_EDGE);
                indicator.setLineHeight(DensityUtils.dp2px(context, 1.5f));
                indicator.setLineWidth(UIUtil.dip2px(context, 16));
                indicator.setYOffset(DensityUtils.dp2px(context, 3));
//                indicator.setXOffset(DensityUtils.dp2px(context, 6));
                indicator.setColors(getResources().getColor(R.color.blue_press));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        viewpager.setOffscreenPageLimit(3);//记数从0开始!!!
        viewpager.setAdapter(new StorePagerAdapter(getSupportFragmentManager()));
        ViewPagerHelper.bind(magicIndicator, viewpager);
    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }


    private class StorePagerAdapter extends FragmentPagerAdapter {

        public StorePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleDataList.get(position);
        }
    }
}
